package com.ywd.materials.admin.context;

import cn.hutool.core.util.ObjectUtil;
import com.ywd.materials.admin.exception.CommonException;
import com.ywd.materials.admin.exception.EmError;
import com.ywd.materials.admin.vo.AdminUserDetailsVo;
import com.ywd.materials.admin.vo.AdminUserVo;

/**
 * 用户缓存信息
 */
public class UserContext {

    private static final ThreadLocal<AdminUserDetailsVo> userHolder = new ThreadLocal<>();

    public static void setUser(AdminUserDetailsVo user) {
        userHolder.set(user);
    }

    public static AdminUserDetailsVo getUser() {
        AdminUserDetailsVo adminUserDetailsVo = userHolder.get();
        if(ObjectUtil.isEmpty(adminUserDetailsVo)){
            throw new CommonException(EmError.AUTH_TOKEN_EXPIRED);
        }
        return adminUserDetailsVo;
    }

    public static AdminUserVo getUserOne() {
        final AdminUserDetailsVo adminUserDetailsVo = userHolder.get();
        if(ObjectUtil.isEmpty(adminUserDetailsVo)){
            throw new CommonException(EmError.AUTH_TOKEN_EXPIRED);
        }
        return adminUserDetailsVo.getUserVo();
    }

    public static void clearUser() {
        userHolder.remove();
    }
}
